<template>
  <div class="user-info">
    <el-row class="person">
      <h1 class="before">人员信息</h1>
      <div class="r-btn p-t" v-if="isShowBtn">
        <el-button type="primary" plain size="small" @click="downFun">下载模板</el-button>
        <el-upload ref="uploadRefXls" class="upload-demo" action="#" :limit="1" :on-change="handleChangeXls" :auto-upload="false"
          :show-file-list="false" :on-exceed="handleExceedXls" accept=".xls,.xlsx">
          <template #trigger>
            <el-button type="primary" size="small">批量导入</el-button>
          </template>
        </el-upload>
      </div>
    </el-row>
    <el-row class="p-t">
      <TableCompEle ref="multipleTable" :tableData="memberList" :tableConfig="tableConfig" :tableAction="isShowBtn ? tableAction : null"
        :isSelect="false" :isIndex="true" :isShowPage="false"></TableCompEle>
      <div class="flex-c" v-if="isShowBtn">
        <el-button @click="addUser" type="primary" plain size="small">
          <i class="el-icon-plus"></i>
          新增成员
        </el-button>
      </div>
    </el-row>
  </div>
</template>

<script>
import { downLoadMemberTemplate, importMember, addMember, delMember } from "@/api/indicatorInput.js"
export default {
  props: {
    isShowBtn: {
      type: Boolean,
      default: true
    },
    projectId: {
      type: String,
      default: ''
    },
    projectStage: {
      type: String,
      default: ''
    },
    memberList: {
      type: Array,
      default: () => []
    },
  },
  data() {
    return {
      tableConfig: [
        {
          isEdit: true,
          isType: 'Input',
          prop: 'memberName',
          placeholder: '请输入',
          label: '姓名'
        },
        {
          isEdit: true,
          isType: 'Select',
          prop: 'memberGender',
          label: '性别',
          placeholder: '请选择',
          options: [
            {
              label: '男',
              value: '男'
            },
            {
              label: '女',
              value: '女'
            }
          ]
        },
        {
          isEdit: true,
          isType: 'Input',
          prop: 'idCard',
          placeholder: '请输入',
          label: '身份证号码'
        },
        {
          isEdit: true,
          isType: 'Input',
          prop: 'profession',
          placeholder: '请输入',
          label: '职称'
        },
        {
          isEdit: true,
          isType: 'Input',
          prop: 'major',
          placeholder: '请输入',
          label: '专业'
        },
        {
          isEdit: true,
          isType: 'Input',
          prop: 'taskDivision',
          placeholder: '请输入',
          label: '任务分工'
        },
        {
          isEdit: true,
          isType: 'Input',
          prop: 'deptName',
          placeholder: '请输入',
          label: '工作单位'
        }
      ],
      tableAction: {
        align: () => 'center',
        width: () => String(120),
        operations: [
          {
            label: '修改',
            isLink: true,
            type: 'primary',
            isShow: row => {
              return !row.isHandle
            },
            method: row => {
              this.$set(row, 'isHandle', true)
            }
          },
          {
            label: '删除',
            isLink: true,
            type: 'danger',
            isShow: row => {
              return !row.isHandle
            },
            method: row => {
              delMember({ memberId: row.memberId }).then(() => {
                this.$emit('initData', this.projectStage)
              })
            }
          },
          {
            label: '保存',
            isLink: true,
            type: 'primary',
            isShow: row => {
              return row.isHandle
            },
            method: row => {
              const _params = row;

              console.log('%c [  ]-150', 'font-size:13px; background:pink; color:#bf2c9f;', this.projectStage)
              Object.assign(_params, { projectId: this.projectId, projectStage: this.projectStage })
              addMember({ ..._params }).then(() => {
                this.$emit('initData', this.projectStage)
              })
            }
          },
          {
            label: '取消',
            isLink: true,
            type: 'primary',
            isShow: row => {
              return row.isHandle
            },
            method: (row, index) => {
              this.del(row, index)
              this.$set(row, 'isHandle', false)
            }
          }
        ]
      }
    }
  },
  methods: {
    addUser() {
      this.memberList.push({
        memberName: '',
        memberGender: '',
        idCard: '',
        profession: '',
        major: '',
        taskDivision: '',
        deptName: '',
        isHandle: true
      })
    },
    del(row) {
      let itemIndex = this.tableData.findIndex(function (obj) {
        return row === obj
      })
      this.tableData.splice(itemIndex, 1)
    },
    downFun() {
      downLoadMemberTemplate()
    },
    // 多次上传覆盖
    handleExceedXls(file) {
      this.$refs.uploadRefXls.clearFiles()
      this.handleChangeXls(file[0])
    },
    handleChangeXls(file) {
      const _file = file?.raw ?? file
      const formData = new FormData()
      formData.append('file', _file)
      formData.append('projectId', this.projectId)
      formData.append('projectStage', this.projectStage)
      importMember({
        file: formData,
        projectId: this.projectId,
        projectStage: this.projectStage
      }).then(() => {
        this.$emit('initData', this.projectStage)
      })
    },
  }
}
</script>

<style lang="less" scoped>
  .upload-demo {
    display: inline-block;
    margin-left: 15px;
  }
</style>
